package com.csgc.camera.biz.other.activity

import android.content.Context
import android.content.Intent
import android.os.Bundle
import androidx.activity.viewModels
import androidx.core.content.ContextCompat
import androidx.core.widget.doAfterTextChanged

import com.csgc.camera.base.BaseActivity
import com.csgc.camera.bean.FeedBackParams

import com.csgc.camera.http.doFailure
import com.csgc.camera.http.doSuccess
import com.csgc.camera.http.viewmodel.AppViewModel
import com.csgc.camera.util.clickWithTrigger
import com.csgc.camera.util.etFilter.RemarkFilter
import com.cysd.spsyxj.R
import com.cysd.spsyxj.databinding.ActivityFeedbackBinding

import com.drake.spannable.addSpan
import com.drake.spannable.span.ColorSpan
import com.gyf.immersionbar.ktx.immersionBar
import com.hjq.toast.ToastUtils

class FeedBackActivity: BaseActivity() {


    companion object{
        fun launch(context:Context){
            context.startActivity(Intent(context,FeedBackActivity::class.java))
        }
    }
    private val viewModel:AppViewModel by viewModels()
    private val binding: ActivityFeedbackBinding by lazy { ActivityFeedbackBinding.inflate(layoutInflater) }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        immersionBar {
            fitsSystemWindows(true)
            //白色状态栏，黑色字体
            statusBarColor(R.color.white)
            statusBarDarkFont(true)

        }
        setContentView(binding.root)

        binding.ivBack.clickWithTrigger { finish() }

        //binding.tvContentTitle.text = "反馈内容".addSpan("(必填)", listOf(ColorSpan(ContextCompat.getColor(this, R.color.color_FF4949))))

        binding.etContent.filters = arrayOf(RemarkFilter(500))
        binding.etContent.doAfterTextChanged {
            val length: Int = it?.toString()?.length?:0
            binding.tvEditCount.text = "$length/500"

        }

        binding.tvSubmit.clickWithTrigger {
            val content = binding.etContent.text.toString().trim()
            if(content.isEmpty()){
                ToastUtils.show("请输入反馈内容")
                return@clickWithTrigger
            }
            val way = binding.etContractWay.text.toString().trim()
            viewModel.submitFeedBack(FeedBackParams(packageName,getString(R.string.app_name),way,content))
        }


        viewModel.feedBackResult.observe(this){result->

            result.doSuccess {
                ToastUtils.show("提交成功")
                finish()
            }
            result.doFailure {
                ToastUtils.show("${it?.message}")
            }
        }

    }
}